package com.aa.springboot_aa.controller;

import com.aa.springboot_aa.pojo.User;
import com.aa.springboot_aa.pojo.dto.Responsemessage;
import com.aa.springboot_aa.pojo.dto.UserDto;
import com.aa.springboot_aa.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

//对外访问的接口
@RestController  //允许接口方法返回对象 转换成json文本
@RequestMapping("/user")  // localhost:8088/user/**  加上user以访问之后的**
public class UserController {

    //被@Service标记后可以在controller里自动装配进来，用userService来接收
    @Autowired
    IUserService userService;


    //Rest


    //增加
    @PostMapping("/add")   //URL: localhost:8088/user/add 前端请求的地址    method: post
    public Responsemessage<User> add(@Validated @RequestBody UserDto user){
    User userNew = userService.add(user);
    return Responsemessage.success(userNew);
    }


    //查询
    @GetMapping("/{Id}")  //URL: localhost:8088/user/1 前端请求的地址    method: get
    public Responsemessage<User> getUser(@PathVariable Integer Id){
        User userNew = userService.getUser(Id);
        return Responsemessage.success(userNew);
    }


    //修改
    @PutMapping("/put")    //URL: localhost:8088/user/put  前端请求的地址    method: put
    public Responsemessage<User> edit(@Validated @RequestBody UserDto user){
        User userNew = userService.edit(user);
        return Responsemessage.success(userNew);
    }


    //删除
    @DeleteMapping("/{Id}")
    public Responsemessage<User> delete(@PathVariable Integer Id){
        userService.delete(Id);
        return Responsemessage.success();
    }
}
